package com.system.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.system.demo.entity.DTO.NewStudentDTO;
import com.system.demo.entity.DTO.StudentIdsRequest;
import com.system.demo.entity.auth.DTO.RoleIdsDTO;
import com.system.demo.service.UserService;
import com.system.demo.tool.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



@RestController
@RequestMapping("/user")
@SaCheckPermission("MemberManagement")
public class UserController {

    @Autowired
    private UserService userService;


    //逐个添加student并赋予账号  必须只有管理员有权限
    @PostMapping("/add")
    public ResponseResult<?> addNewUserByOnce(@RequestBody NewStudentDTO newStudentDTO) {
        return userService.addNewUserByOnce(newStudentDTO);
    }


    //下载批量导入学生账号excel示例模板
    @GetMapping("/excel")
    public ResponseEntity<?> getExcelTemplate(){
        return userService.getExcelTemplate();
    }

    //输入excel形式化数据 批量添加student
    @PostMapping("/batchAdd")
    public ResponseResult<?> batchAddStudents(@RequestParam("file") MultipartFile file){
        return userService.batchAddStudents(file);
    }

    @DeleteMapping
    public ResponseResult<?> deleteStudents(@RequestBody StudentIdsRequest studentIds) {
        return userService.deleteStudents(studentIds);
    }

    //根据筛选条件查询用户
    //有哪些筛选条件呢：用户角色、根据用户的uid（学生就是学号，管理就是教职工号）、班级（格式2102）、姓名
    @GetMapping
    public ResponseResult<?> getUsersByLimits(@RequestParam String role,
                                              @RequestParam String uid,
                                              @RequestParam String stuClass,
                                              @RequestParam String name,
                                              @RequestParam String current, @RequestParam String size){
        return userService.getUsersByLimits(role, uid, stuClass, name,  current, size);
    }


    //修改用户的角色
    @PostMapping("/reviseRole")
    public ResponseResult<?> reviseRoleForUser(@RequestParam String uid, @RequestBody RoleIdsDTO roleIdsDTO){
        return userService.reviseRoleForUser(uid, roleIdsDTO);
    }


    //重置账户（应用场景：比如用户忘记了密码，邮箱也没有，就算绑定了邮箱，邮箱不可用！这时候可以重置账号成最原始的状态 即默认密码 无邮箱）
    @GetMapping("/reset")
    public ResponseResult<?> resetUser(@RequestParam String uid){
        return userService.resetUser(uid);
    }


    //根据用户uid返回当前所具有的role
    @GetMapping("/getRole")
    public ResponseResult<?> getRoleListForUid(@RequestParam String uid){
        return userService.getRoleListForUid(uid);
    }
}
